#coding=utf8
import struct

from network import LEN_HEADER,LEN_HEADER_SIZE,CMD_MARK,CMD_MARK_SIZE
from network import protocode

def get_enum(msg_name):
	return msg_name.upper()

def _to_packet(msg):
	data = msg.SerializeToString()
	length = struct.pack(LEN_HEADER,len(data))
	msg_name = msg.DESCRIPTOR.name
	enum_var = get_enum(msg_name)
	msg_cmd = getattr(protocode,enum_var)
	cmd_mark = struct.pack(CMD_MARK,msg_cmd)
	return length+cmd_mark+data

def send(sock,msg):
	sock.sendall(_to_packet(msg))
